package com.example.estest.util;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class FileUtils {

    /**
     *FileUtils.writeString
     * @param pathAndName  路径和名字
     * @param s // 数据
     */
    public static void writeString(String pathAndName , String s) {
        File file = new File(pathAndName); // "D://attackCount2"
        try {
            org.apache.commons.io.FileUtils.writeStringToFile(file, s+"\r\n", "utf-8");
        } catch (IOException e) {
        }
    }


    public static void writeStringadd(String pathAndName , String s) {
        File file = new File(pathAndName); // "D://attackCount2"
        try {
            org.apache.commons.io.FileUtils.writeStringToFile(file, s+"\r\n", "utf-8",true);
        } catch (IOException e) {
        }
    }

    public static String readString(String path) throws IOException {
        File file = new File(path);
        StringBuilder stringBuilder = new StringBuilder();

        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        }

        return stringBuilder.toString();
    }

    // 遍历目录
    public static void visitAllDirsAndFiles(File dir,List<String> list) {
        list.add(dir.getAbsolutePath());
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                visitAllDirsAndFiles(new File(dir, children[i]),list);
            }
        }
    }

    /**
     * 文件转换set
     *
     * @return
     * @paramfilePath
     */
    public static Set<String> fileTransfromToSet(String filePath) {

        File file = new File(filePath);

        try (InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file))) {

            String dict;
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            Set<String> list = new HashSet<>();
            while ((dict = bufferedReader.readLine()) != null) {
                list.add(dict);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new HashSet<>();
    }


    /**
     * 文件转换set
     *
     * @return
     * @paramfilePath
     */
    public static List<String> fileTransfromToList(String filePath) {

        File file = new File(filePath);

        try (InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file))) {

            String dict;
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            List<String> list = new LinkedList<>();
            while ((dict = bufferedReader.readLine()) != null) {
                list.add(dict);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static void createDirectory(String directoryPath,String errorPath) {
        // 使用 Paths.get() 创建 Path 对象
        Path path = Paths.get(directoryPath);

        try {
            // 使用 Files.createDirectory() 创建文件夹
            Files.createDirectory(path);
            System.out.println("Directory created successfully.");
        } catch (IOException e) {
            writeStringadd(errorPath,directoryPath+"-------文件创建失败");
            // 处理异常
            System.err.println("Error creating directory: " + e.getMessage());
        }
    }
}
